package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.dao.CategoryMapper;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageResult findPage(Integer page, Integer size, Map<String, String> searchMap) {
        LambdaQueryWrapper<Category> wrapper = Wrappers.<Category>lambdaQuery();

        if (searchMap.get("name") != null && searchMap.get("name").length() > 0) {
            wrapper.like(Category::getName, searchMap.get("name"));
        }
        if (searchMap.get("isShow") != null) {
            wrapper.eq(Category::getIsShow, searchMap.get("isShow"));
        }
        if (searchMap.get("isMenu") != null) {
            wrapper.eq(Category::getIsMenu, searchMap.get("isMenu"));
        }
        if (searchMap.get("seq") != null) {
            wrapper.eq(Category::getSeq, searchMap.get("seq"));
        }
        if (searchMap.get("parentId") != null) {
            wrapper.eq(Category::getParentId, searchMap.get("parentId"));
        }
        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(page, size), wrapper);
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setTotal(categoryPage.getTotal());
        categoryPageResult.setRows(categoryPage.getRecords());
        return categoryPageResult;

    }
}
